package com.ruoyi.common.utils;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池任务
 *
 * @author heyingcheng
 * @date 2018/9/11 23:08
 */
public class ThreadPoolUtils {

    private ThreadPoolUtils() {
        throw new IllegalStateException("Utility class");
    }

    public static ThreadPoolTaskExecutor create1ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(1);
    }

    public static ThreadPoolTaskExecutor create2ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(2);
    }

    public static ThreadPoolTaskExecutor create3ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(3);
    }

    public static ThreadPoolTaskExecutor create4ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(4);
    }

    public static ThreadPoolTaskExecutor create5ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(5);
    }

    public static ThreadPoolTaskExecutor create8ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(8);
    }

    public static ThreadPoolTaskExecutor create10ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(10);
    }

    public static ThreadPoolTaskExecutor create15ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(15);
    }

    public static ThreadPoolTaskExecutor create20ThreadPoolTaskExecutor() {
        return createThreadPoolTaskExecutor(20);
    }

    public static ThreadPoolTaskExecutor createThreadPoolTaskExecutor(int corePoolSize) {
        return createThreadPoolTaskExecutor(corePoolSize, corePoolSize);
    }

    public static ThreadPoolTaskExecutor createThreadPoolTaskExecutor(String threadNamePrefix, int corePoolSize) {
        return createThreadPoolTaskExecutor(threadNamePrefix, corePoolSize, corePoolSize);
    }

    public static ThreadPoolTaskExecutor createThreadPoolTaskExecutor(int corePoolSize, int maxPoolSize) {
        return createThreadPoolTaskExecutor(null, corePoolSize, maxPoolSize);
    }

    public static ThreadPoolTaskExecutor createThreadPoolTaskExecutor(String threadNamePrefix, int corePoolSize, int maxPoolSize) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(5000);
        threadPoolTaskExecutor.setKeepAliveSeconds(300);
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(false);
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskExecutor.initialize();
        if (threadNamePrefix != null) {
            threadPoolTaskExecutor.setThreadNamePrefix(threadNamePrefix);
        }
        return threadPoolTaskExecutor;
    }

}
